#include "StdAfx.h"

CFont* Tooltip::mFontTitle = NULL;
CFont* Tooltip::mFontMessage = NULL;
Texture* Tooltip::mBorderTexture = NULL;
Texture* Tooltip::mBackground = NULL;

Tooltip::Tooltip() : mTitle(""), mMessage("")
{
	if(mFontTitle == NULL)
	{
		mFontTitle = new CFont("Fonts\\ArialN.ttf", "Arial Narrow", 8, 16, true);
		mFontMessage = new CFont("Fonts\\ArialN.ttf", "Arial Narrow", 7, 14);
		mBorderTexture = sTexture.GetTexture("Interface\\Tooltips\\UI-Tooltip-Border.blp");
		mBackground = sTexture.GetTexture("Interface\\Tooltips\\UI-Tooltip-Background.blp");

		if(mBorderTexture == NULL)
		{
			StartupException e = 
			{
				"Missing texture",
				"Unable to load border texture for tooltips!"
			};

			throw e;
		}
	}
}

void Tooltip::Draw(ui32 x, ui32 y)
{
	if(mMessage == "" && mTitle == "")
		return;

	DrawBorder(x - 10, y - 10);
	mFontTitle->DrawText(x, y, 250, 30, mTitle, 0xFFFFB90F);
	mFontMessage->DrawTextA(x, y + 25, 250 / mFontMessage->GetXAspect(), GetBodyHeight() / mFontMessage->GetYAspect(), mMessage, 0xFFFFFFFF, DT_WORDBREAK);
}

void Tooltip::CalcBorderElems()
{
	mElemsHorizontal = 15;
	ui32 h = GetBodyHeight(true) + 25 + 20;
	mElemsVertical = (ui32)ceil((h - 30) / 16.0f);
}

void Tooltip::DrawBorder(ui32 x, ui32 y)
{
	DrawBackground(x, y);
	DrawTopEdges(x, y);
	DrawBottomEdges(x, y);
	DrawTopBorder(x + 15, y);
	DrawSideBorder(x, y + 15);
}

void Tooltip::DrawTopEdges(ui32 x, ui32 y)
{
	_Vertex3FRHWT vertLeft[4] = 
	{
		{ x, y, 0, 1, 65.0f / 128.0f, 1.0f / 16.0f },
		{ x + 15, y, 0, 1, 80.0f / 128.0f, 1.0f / 16.0f },
		{ x + 15, y + 15, 0, 1, 80.0f / 128.0f, 1 },
		{ x, y + 15, 0, 1, 65.0f / 128.0f, 1 },
	};

	sD3D->SetTexture(0, *mBorderTexture);
	sD3D->SetFVF(Vertex3FRHWT::FVF);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, vertLeft, sizeof(_Vertex3FRHWT));
	

	_Vertex3FRHWT vertRight[4] = 
	{
		{ x + 15 + mElemsHorizontal * 16, y, 0, 1, 80.0f / 128.0f, 1.0f / 16.0f },
		{ x + 15 + mElemsHorizontal * 16 + 15, y, 0, 1, 95.0f / 128.0f, 1.0f / 16.0f },
		{ x + 15 + mElemsHorizontal * 16 + 15, y + 15, 0, 1, 95.0f / 128.0f, 1 },
		{ x + 15 + mElemsHorizontal * 16, y + 15, 0, 1, 80.0f / 128.0f, 1 },
	};

	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, vertRight, sizeof(_Vertex3FRHWT));
	sD3D->SetTexture(0, NULL);
}

void Tooltip::DrawBottomEdges(ui32 x, ui32 y)
{
	y = y + 15 + mElemsVertical * 16;
	_Vertex3FRHWT vertLeft[4] = 
	{
		{ x, y, 0, 1, 97.0f / 128.0f, 0 },
		{ x + 15, y, 0, 1, 112.0f / 128.0f, 0 },
		{ x + 15, y + 15, 0, 1, 112.0f / 128.0f, 15 / 16.0f },
		{ x, y + 15, 0, 1, 97.0f / 128.0f, 15 / 16.0f },
	};

	sD3D->SetTexture(0, *mBorderTexture);
	sD3D->SetFVF(Vertex3FRHWT::FVF);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, vertLeft, sizeof(_Vertex3FRHWT));
	

	_Vertex3FRHWT vertRight[4] = 
	{
		{ x + 15 + mElemsHorizontal * 16, y, 0, 1, 112.0f / 128.0f, 0 },
		{ x + 15 + mElemsHorizontal * 16 + 15, y, 0, 1, 127.0f / 128.0f, 0 },
		{ x + 15 + mElemsHorizontal * 16 + 15, y + 15, 0, 1, 127.0f / 128.0f, 15.0f / 16.0f },
		{ x + 15 + mElemsHorizontal * 16, y + 15, 0, 1, 112.0f / 128.0f, 15.0f / 16.0f },
	};

	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, vertRight, sizeof(_Vertex3FRHWT));
	sD3D->SetTexture(0, NULL);
}

void Tooltip::DrawTopBorder(ui32 x, ui32 y)
{
	_Vertex3FRHWT vertTop[4] =
	{
		{ x, y, 0, 1, 33.0f / 128.0f, 0 },
		{ x + mElemsHorizontal * 16, y, 0, 1, 33.0f / 128.0f, 1 },
		{ x + mElemsHorizontal * 16, y + 16, 0, 1, 49.0f / 128.0f, 0 },
		{ x, y + 16, 0, 1, 49.0f / 128.0f, 1 }
	};

	sD3D->SetTexture(0, *mBorderTexture);
	sD3D->SetFVF(Vertex3FRHWT::FVF);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, vertTop, sizeof(_Vertex3FRHWT));
	sD3D->SetTexture(0, NULL);

	y += 15 + mElemsVertical * 16;

	_Vertex3FRHWT vertBottom[4] = 
	{
		{ x, y, 0, 1, 48.0f / 128.0f, 0 },
		{ x + mElemsHorizontal * 16, y, 0, 1, 48.0f / 128.0f, 1 },
		{ x + mElemsHorizontal * 16, y + 16, 0, 1, 64.0f / 128.0f, 0 },
		{ x, y + 16, 0, 1, 64.0f / 128.0f, 1 }
	};

	sD3D->SetTexture(0, *mBorderTexture);
	sD3D->SetFVF(Vertex3FRHWT::FVF);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, vertBottom, sizeof(_Vertex3FRHWT));
	sD3D->SetTexture(0, NULL);
}

void Tooltip::DrawSideBorder(ui32 x, ui32 y)
{
	_Vertex3FRHWT vertLeft[4] =
	{
		{ x, y, 0, 1, 1 / 128.0f, 0 },
		{ x + 16, y, 0, 1, 17.0f / 128.0f, 0 },
		{ x + 16, y + mElemsVertical * 16 + 1, 0, 1, 17.0f / 128.0f, 1 },
		{ x, y + mElemsVertical * 16 + 1, 0, 1, 1 / 128.0f, 1 },
	};

	sD3D->SetTexture(0, *mBorderTexture);
	sD3D->SetFVF(Vertex3FRHWT::FVF);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, vertLeft, sizeof(_Vertex3FRHWT));
	sD3D->SetTexture(0, NULL);

	x += 15 + mElemsHorizontal * 16;

	_Vertex3FRHWT vertRight[4] =
	{
		{ x, y, 0, 1, 16 / 128.0f, 0 },
		{ x + 16, y, 0, 1, 32.0f / 128.0f, 0 },
		{ x + 16, y + mElemsVertical * 16 + 1, 0, 1, 32.0f / 128.0f, 1 },
		{ x, y + mElemsVertical * 16 + 1, 0, 1, 16 / 128.0f, 1 },
	};

	sD3D->SetTexture(0, *mBorderTexture);
	sD3D->SetFVF(Vertex3FRHWT::FVF);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, vertRight, sizeof(_Vertex3FRHWT));
	sD3D->SetTexture(0, NULL);
}

void Tooltip::DrawBackground(ui32 x, ui32 y)
{
	_Vertex3FRHWT vertBackg[4] = 
	{
		{ x + 2, y + 2, 0, 1, 0, 0 },
		{ x + 30 + mElemsHorizontal * 16 - 2, y + 2, 1, 0 },
		{ x + 30 + mElemsHorizontal * 16 - 2, y + 30 + mElemsVertical * 16 - 2, 1, 1 },
		{ x + 2, y + 30 + mElemsVertical * 16 - 2, 0, 1 },
	};

	sD3D->SetTexture(0, *mBackground);
	sD3D->SetFVF(Vertex3FRHWT::FVF);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, vertBackg, sizeof(_Vertex3FRHWT));
	sD3D->SetTexture(0, NULL);
}